//接口地址
//var interURL = 'http://192.168.0.187:8044/';
var interURL = 'http://112.25.171.120:8044/';

var obj = $('form input,select,form textarea');//表单对象

//显示删除图标
function getVal(obj) {
    var searVal = $.trim($(obj).val());
    if (searVal.length > 0) {
        $(obj).next().show();
    } else {
        $(obj).next().hide();
    }
}

//点击删除图标
function delVal(obj) {
    $(obj).prev().val('');
    $('.list-container').html('');
    $('.infinite-scroll-preloader').hide();
    $(obj).hide();
    //如果有提交按钮
    if($('#submit').length>0){
        $('#submit')[0].style.display="none";
        $('#submit-hui')[0].style.display="block";
    }
}

//input校验
function verifyInput(obj) {
    var value = $.trim($(obj).val());
    if (value == '') {
       // $(obj).css({"border": "1px solid #0ab690"})
       // $.toast('必填项不能为空', 2500, '', 220);
        return false;
    } else {
        $(obj).css({"border": "0"})
    }
    return true;
}

//表单输入校验
function verify() {
    var flag = true;
    $.each(obj, function (index, item) {
        if ($(item)[0].type == 'text' || $(item)[0].type == 'textarea') {
            if (!verifyInput(item)) {
                flag = false;
                return false;
            }
        }
    });
    if (!flag) {
        return false;
    }
    return true;
}

//提交表单
$('#submit').on('click', function (e) {
    var interAPI = $(this).attr('interAPI');//接口地址
    var type = $(this).attr('type');//接口类型
    //表单验证
    if (!verify()) {
        return false;
    }
    if (type == 1) {
        var name = $(this).attr('name');//提交名称
        var orderid = getUrlParam("orderid");
        var type = getUrlParam("type");
        var returnURL = '';
        if (name == 'evaluate') {
            //评价工单
            var dataArr = {
                "orderid": orderid,
                "replystatu": $(obj).eq(0).val(),
                "replycontent": $(obj).eq(1).val(),
            };
            returnURL = 'tabsduo.html';
        }else{			
            //微信绑定
            var openid = getUrlParam("opid");
            sessionStorage.setItem('openid',openid);
			if(openid=='null'){
				alert('请传入openID');
			}
            var dataArr = {
                "userphone": $(obj).eq(0).val(),
                "openid": openid,
                "verifycode": $(obj).eq(1).val(),
            };
            //根据来源不同，调整到不同的目标页面
            if(type=="jilu"){
                returnURL = "tabsduo.html";
            }
            if(type=="tousu"){
                returnURL = "xuzhi.html";
            }

        }
        //普通表单提交的ajax请求
        $.ajax({
            url: interURL + interAPI,
            type: "post",
            dataType: "json",
            data: dataArr,
            async: false,
            success: function (returnJSONN) {
                if (returnJSONN.code == '1') {
                    $.toast(returnJSONN.msg, 2500, '', 220);
                    setTimeout(function () {
                        location.href = returnURL
                    }, 2500);
                } else {
                    $.toast(returnJSONN.msg, 2500, '', 220);
                    return false;
                }
            }
        });
        return false;
    }

    //文件上传类型的ajax请求
    $(".loader-box").css("display","");
    $("#submit-hui").css("display","");
    $("#submit").css("display","none");

   // return false;
    e.preventDefault();
    var myform = document.querySelector("form");
    var formData = new FormData(myform);
	formData.append("userid",localStorage.getItem('userid'));
    for(var i =0;i<filearr.length;i++){    //提交时，我们把filearr中的数据遍历一遍
        formData.append("file[]", filearr[i]); //用append添加到formData中，就得用户最重要提交的图片了
    };    
    $.ajax({
        url: interURL + interAPI,
        type: "post",
        dataType: "json",
        data: formData,
        async: false,
        contentType: false,
        processData: false,
        success: function (returnJSONN) {
            if (returnJSONN.code == '1') {
                localStorage.setItem('reopenid',getUrlParam("opid"));
               location.href = "success.html";
            } else {
                $.toast(returnJSONN.msg, 2500, '', 220);
                return false;
            }
        }
    });
});

obj.on('keyup',function(){
    if (verify()) {
        $('#submit')[0].style.display="block";
        $('#submit-hui')[0].style.display="none";
    }else{
        $('#submit')[0].style.display="none";
        $('#submit-hui')[0].style.display="block";
    }
});

//验证码倒计时
var wait=60;
function time(o) {
    if (wait == 0) {
        o.removeAttribute("disabled");
        o.value="获取验证码";
        $(o).addClass('button-danger');
        wait = 60;
    } else {
        o.setAttribute("disabled", true);
        o.value="重新发送(" + wait + ")";
        $(o).removeClass('button-danger');
        o.style.backgroundColor="#81d7c4";
        wait--;
        setTimeout(function() {
            time(o)
        },
        1000)
    }
}


//发送验证码
function getcode(phone,obj, interAPI) {
    var dataArr = {
        "phonenumber": phone,
        "type": 3
    };
    $.ajax({
        url: interURL + interAPI,
        type: "post",
        dataType: "json",
        data: dataArr,
        async: false,
        success: function (returnJSONN) {
            if (returnJSONN.code == '1') {
                time(obj[0]);
                $.toast(returnJSONN.msg, 2500, '', 220);
            } else {
                $.toast(returnJSONN.msg, 2500, '', 220);
                return false;
            }
        }
    });
}

//通知公告详情
$(".list-container").on("click", "li", function () {
    var noticeID = $(this).attr('noticeID');
    var type = $(this).parent().attr('type');//0通知公告，1工单列表
    var interAPI = $(this).parent().attr('interAPI');
    var returnURL; //跳转地址
    if (type == 0) {
        var dataArr = {
            "newsid": noticeID
        }
        returnURL = 'notice-show.html';
    } else {
        var dataArr = {
            "orderid": noticeID
        }
        returnURL = 'tabsduo-show.html?orderid=' + noticeID;
    }

    $.ajax({
        url: interURL + interAPI,
        type: "post",
        dataType: "json",
        data: dataArr,
        success: function (returnJSONN) {
            if (returnJSONN.code == '1') {
                sessionStorage.setItem('noticeInfo', JSON.stringify(returnJSONN.data));
                location.href = returnURL;
            } else {
                $.toast(returnJSONN.msg, 2500, '', 220);
                return false;
            }
        }
    });
});


//跳转到评价界面
$("#content-list").on("click", ".evaluate", function () {
    var orderID = $(this).attr("orderID");
    location.href = "evaluate.html?orderid=" + orderID;
});

//工单列表-其他操作
$("#content-list").on("click", ".other", function () {
    $.toast($(this).attr("orderID"), 2500, '', 220);
});

//获取url参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

//多图浏览
$(document).on('click','.albumImgs',function () {
    var imgObj = $(this).find('img');
    var imgList = [];
    for(var i=0; i<imgObj.length; i++){
        imgList.push(imgObj.attr('src'));
    }
    $.photoBrowser({
        photos : imgList,
        type: 'popup'
    }).open();
    $('.photo-browser-close-link').html('关闭').removeClass('icon-left ').css({"float":"right","font-size":".85rem"});
    //myPhotoBrowserPopup.open(); //原来的
});

//检测是否绑定手机
$(function(){
    $('#content').hide();
    var type = getUrlParam("type"); //通过投诉来绑定还是通过投诉记录来绑定
    var openid = getUrlParam("openid");
    var sopid = sessionStorage.getItem("openid");
    if(openid!==null || sopid!==null){
        //检测是否绑定
        $.ajax({
            url: interURL + 'api/wxapi/users/getuserid',
            type: "post",
            dataType: "json",
            data: {
                  "openid": openid
                },
            success: function (returnJSONN) {
                if (returnJSONN.code == '0') {
                    location.href = './complain.html?type='+type+'&opid='+openid;
                    return false;
                }else{
                    $('#content').show();
                   // alert(returnJSONN.data);
                    localStorage.setItem('userid',returnJSONN.data);
                    return false;
                }
            }
        });
    }
});
